import Router from 'koa-router';
import { routes } from '../../routes';
import { validationMiddleware } from '../../middleware/validationMiddleware';
import { ProductDto } from './Product.dto';
import {
  addProductService,
  addProductSkuService,
  getProductListService,
  getProductSkuById
} from '../../service/ProductService';
import { ProductSkuDto } from './ProductSku.dto';

const router = new Router({
  prefix: '/api'
});

router.post(routes.ADMIN_PRODUCT_ADD, validationMiddleware(ProductDto), async ctx => {
  const body: any = ctx.request.body;
  let product = body;
  let token = ctx.headers['authorization'];
  return ctx.body = await addProductService(product, token);
});

router.post(routes.ADMIN_PRODUCT_SKU_ADD, async ctx => {
  const body: any = ctx.request.body;
  let { productId, skuList } = body;
  return ctx.body = await addProductSkuService(productId, skuList);
});

router.get(routes.ADMIN_PRODUCT_LIST, async ctx => {
  let token = ctx.headers['authorization'];
  return ctx.body = await getProductListService(token, ctx.query);
});

router.get(routes.ADMIN_PRODUCT_SKU_BY_ID, async ctx => {
  const { productId } = ctx.query;
  return ctx.body = await getProductSkuById(Number(productId));
});


export default router.routes();